﻿//-----------------------------------------------------------------------
// <copyright file="BusinessBase.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using GateShift.Common.Repository.Interfaces.DataInterfaces.Base;
using GateShift.Common.Repository.Interfaces.Factories;
using GateShift.Common.Repository.Interfaces.Repositories.Base;

#endregion

namespace GateShift.Common.Repository.Business
{
    /// <summary>
    /// Business base class for repository based objects
    /// </summary>
    /// <typeparam name="D">IData based source for data</typeparam>
    /// <typeparam name="R">IRepository based source for actions</typeparam>
    public abstract class BusinessBase<D, R>
        where D : class, IData
        where R : class, IRepository<D>
    {
        #region CTor/DTor

        /// <summary>
        /// Initializes a new instance of the BusinessBase class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="data">Data for backing the business object</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory or Data are null</exception>
        protected BusinessBase(IRepositoryFactory factory, D data)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Cannot perform actions without a factory");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data", "Cannot perform actions without a data reference");
            }

            Repository = RetrieveRepository(factory);
            Data = data;
            Factory = factory;
        }

        /// <summary>
        /// Initializes a new instance of the BusinessBase class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory is null</exception>
        protected BusinessBase(IRepositoryFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Cannot retrieve data without a factory");
            }

            Repository = RetrieveRepository(factory);
            Data = Repository.Create();
            Factory = factory;
        }

        #endregion

        #region State Management Properties

        /// <summary>
        /// Gets a value indicating whether or not the object has been modified
        /// </summary>
        public bool IsDirty
        {
            get
            {
                return Data.IsDirty;
            }
        }

        /// <summary>
        /// Gets a value indicating whether or not the object is new and unsaved
        /// </summary>
        public bool IsNew
        {
            get
            {
                return Data.IsNew;
            }
        }

        /// <summary>
        /// Gets a value indicating whether or not the object has been deleted
        /// </summary>
        public bool IsDeleted
        {
            get
            {
                return Data.IsDeleted;
            }
        }

        #endregion

        #region Storage Properties

        /// <summary>
        /// Gets or sets the IRepository reference for actions dealing with queries and object management
        /// </summary>
        protected R Repository { get; set; }

        /// <summary>
        /// Gets or sets the IData reference for the backing information for this business class
        /// </summary>
        protected D Data { get; set; }

        /// <summary>
        /// Gets or sets the IRepositoryFactory reference for the backing information for this business class
        /// </summary>
        protected IRepositoryFactory Factory { get; set; }

        #endregion

        #region Validation

        /// <summary>
        /// Method to check whether or not the current object is valid
        /// </summary>
        /// <returns>Boolean based on the validity of the object</returns>
        public abstract bool IsValid();

        #endregion

        #region Operations

        /// <summary>
        /// Makes a call to the Repository to save the object
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown if the object is currently in an invalid state</exception>
        public void Save()
        {
            if (!IsValid())
            {
                throw new InvalidOperationException("Current state of the object cannot be saved. Check IsValid first.");
            }

            Repository.Save(Data);
        }

        /// <summary>
        /// Makes a call to the Repository to delete the object
        /// </summary>
        public void Delete()
        {
            Repository.Delete(Data);
        }

        #endregion

        #region Factory Methods

        /// <summary>
        /// Calls the repository to retrieve the instance for data access calls
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <returns>IRepository for the Data type of this class</returns>
        protected static R RetrieveRepository(IRepositoryFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Must have a factory to retrieve a repository from");
            }

            return factory.GetRepository<R>();
        }

        #endregion
    }
}